import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

import javax.swing.table.*;

import java.text.SimpleDateFormat;
import java.util.*;


public class MainWindow extends JFrame implements ActionListener, SavedListener {

	private static final long serialVersionUID = 1L;
	
	Bridget briget;
	
	/* this window */
	private JPanel m_statusPicture;
	private JPanel m_statusEaten;
	private JPanel m_statusFitness;
	private JPanel m_mainPanel;
	
	/* menu */
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem settingsItem;
	private JMenuItem goalItem;
	private JMenuItem journalItem;
	
	/* other windows */
	SettingsWindow settingsWindow;
	GoalsWindow goalsWindow;
	JournalWindow journalWindow;	
	
	
	public MainWindow(Bridget bridget) {
		super("Bridget Johnes: diet monitor");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	//	this.bridget = bridget;
		this.setPreferredSize(new Dimension(280, 235));
		/* Creating panel with status picture */
		m_statusPicture = new JPanel();
		m_statusPicture.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		m_statusPicture.setLayout(new BoxLayout(m_statusPicture, BoxLayout.LINE_AXIS));
		
		Date date=new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
		String s= formatter.format(date);
		JLabel name = new JLabel("Your current status");
		JLabel dateLabel = new JLabel("on "+s);
		name.setAlignmentX(CENTER_ALIGNMENT);
		dateLabel.setAlignmentX(CENTER_ALIGNMENT);
		JLabel BMILabel = new JLabel("BMI:  ");
		JLabel statusPictureLabel = new JLabel("                                                                                              " +
				"                ");
		statusPictureLabel.setOpaque(true);
		 if (bridget.statusBMI()==0)
		    {
			 
			 statusPictureLabel.setBackground(Color.green);
			 System.out.println("sdggggggggggggggggggggggggg");
			 

		    }
		    else
		        if (bridget.statusBMI()==-1)
		        {
		        	statusPictureLabel.setBackground(Color.yellow);
		        	
		        }
		    else
		        if (bridget.statusBMI()==-2)
		        {
		        	statusPictureLabel.setBackground(Color.red);

		        }
		    else
		        if (bridget.statusBMI()==1)
		        {
		        	statusPictureLabel.setBackground(Color.yellow);

		        }
		    else
		        if (bridget.statusBMI()==2)
		        {
		        	statusPictureLabel.setBackground(Color.red);
		        }
		    else
		        if (bridget.statusBMI()==3)
		        {
		        	statusPictureLabel.setBackground(Color.red);

		        }
		    else
		        if (bridget.statusBMI()==4)
		        {
		        	statusPictureLabel.setBackground(Color.red);
		        }
		
		

		 m_statusPicture.add(BMILabel);
		 m_statusPicture.add(statusPictureLabel);
		
		
		/* Creating panel with table with eaten calories */
		m_statusEaten = new JPanel();
		m_statusEaten.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		m_statusEaten.setLayout(new BoxLayout(m_statusEaten, BoxLayout.PAGE_AXIS));
		JLabel statusEatenLabel = new JLabel("Diet");
		statusEatenLabel.setAlignmentX(CENTER_ALIGNMENT);
		


		String[] columnNames = {"Consumed energy",
                "Goal Energy"};

		Object[][] data = {
				{bridget.getConsEnergy(), bridget.getGoalEnergy()}};


		final JTable table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(10, 16));
	
		JScrollPane scrollPane = new JScrollPane(table);
	
		
		m_statusEaten.add(statusEatenLabel);
		m_statusEaten.add(scrollPane);
		
		// Creating panel with table with fitness results 
		m_statusFitness = new JPanel();
		m_statusFitness.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		m_statusFitness.setLayout(new BoxLayout(m_statusFitness, BoxLayout.PAGE_AXIS));
		JLabel statusFitnessLabel = new JLabel("Fitness");
		statusFitnessLabel.setAlignmentX(CENTER_ALIGNMENT);
		
		
				String[] column = {"Spend energy",
                "Fitness plan"};

		Object[][] data1 = {
				{bridget.getSpendEnergy(), bridget.getFitgoal()}};


		final JTable table1 = new JTable(data1, column);
	
	
	
		table1.setPreferredScrollableViewportSize(new Dimension(10, 16));
		JScrollPane scrollPanel1 = new JScrollPane(table1);
		m_statusFitness.add(statusFitnessLabel);
		m_statusFitness.add(scrollPanel1);
		
		
		
		/* Creating panel with status picture */
		m_mainPanel = new JPanel();
		m_mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		m_mainPanel.setLayout(new BoxLayout(m_mainPanel, BoxLayout.PAGE_AXIS));
		m_mainPanel.add(name);
		m_mainPanel.add(dateLabel);
		m_mainPanel.add(m_statusPicture);
		//m_mainPanel.add(scrollPane);
		//m_mainPanel.add(scrollPanel1);
		m_mainPanel.add(m_statusEaten);
		m_mainPanel.add(m_statusFitness);
		
		
		/* Creating menu */
		menuBar = new JMenuBar();
		menu = new JMenu("Menu");
		menuBar.add(menu);
		settingsItem = new JMenuItem("Personal settings");
		settingsItem.setActionCommand("show settings");
		settingsItem.addActionListener(this);
		goalItem = new JMenuItem("Set goal");
		goalItem.setActionCommand("show goal");
		goalItem.addActionListener(this);
		journalItem = new JMenuItem("Journal");
		journalItem.setActionCommand("show journal");
		journalItem.addActionListener(this);
		menu.add(settingsItem);
		menu.add(goalItem);
		menu.add(journalItem);
		
		
		/* Creating other windows */
		settingsWindow = new SettingsWindow(bridget);
		settingsWindow.addSavedListener(this);
		goalsWindow = new GoalsWindow(bridget);
		goalsWindow.addSavedListener(this);
		journalWindow = new JournalWindow(bridget);
		
		
		System.out.println("constructor performed");
		setJMenuBar(menuBar);
		setContentPane(m_mainPanel);
		pack();
	}
	
	
	public void actionPerformed(ActionEvent e) {
		System.out.println("action performed");
		
		if ("show settings".equals(e.getActionCommand())) {
			settingsWindow.updateFields();
			settingsWindow.setVisible(true);
		} else if ("show goal".equals(e.getActionCommand())) {
			goalsWindow.setVisible(true);
		} else if ("show journal".equals(e.getActionCommand())) {
			journalWindow.setVisible(true);
		}
		
	}
		
	
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	Bridget bridget = new Bridget();
            	Professions prof = new Professions();
                new MainWindow(bridget).setVisible(true);
            }
		});
	}


	public void saved(SavedEvent e) {
		if (e.name.equals("settings")) {
			System.out.println("MainWindow received event that settings have been changed");
		} else if (e.name.equals("goals")) {
			System.out.println("MainWindow received event that goals have been changed");
		} else if (e.name.equals("journal")) {
			System.out.println("MainWindow received event that journal has been changed");
		}
	}
}
